package com.powerbridge.example.controller;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.powerbridge.example.config.ExampleConfig;
import com.powerbridge.example.domain.Echo;
import com.powerbridge.example.exception.ExceptionExample;
import com.powerbridge.example.service.EchoService;
import com.powerbridge.platform.core.constant.SysStatus;
import com.powerbridge.platform.core.controller.BaseController;
import com.powerbridge.platform.core.msg.ObjectRestResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.ResponseHeader;

/**
 * 控制层example
 * shunhe@powerbridge.com
 * 2017年11月3日上午10:56:30
*/
@Component
@Path("/echo")
@Api(value = "Echo Resource")
public class EchoController extends BaseController<EchoService, Echo>{
	
	private final Logger LOG = LoggerFactory.getLogger(this.getClass());
	
	@Context
	protected HttpServletRequest request;  
	
	@Autowired
	private EchoService echoBiz;
	
	@Autowired
	private ExampleConfig exampleConfig;
	
	@GET
	@Path("/hello")
	@Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "打印hello springboot")
    @ApiResponses(value = {
        @ApiResponse(code = 201, message = "测试成功", response = JSONObject.class, responseHeaders = {
                @ResponseHeader(name = "Location", description = "", response = String.class)
        }),
        @ApiResponse(code = 500, message = "测试出现异常", response = Exception.class)
    })
	public Response hello(@Context UriInfo uriInfo, @QueryParam("what") String what) throws ExceptionExample{
		LOG.info("batch.a="+exampleConfig.getBatchA());
		LOG.info("batch.b="+exampleConfig.getBatchB());
		LOG.info("port="+exampleConfig.getPort());
		if(what==null||!what.toLowerCase().equals("springboot")) {
			throw new ExceptionExample("{msg:exception,code:100001}");
		}
		ObjectRestResponse objectRestResponse = new ObjectRestResponse<Echo>().code(SysStatus.PB_STATUS_HELLO.getStatus()).msg(SysStatus.PB_STATUS_HELLO.getMsg()).rel(true);
		return getResponseBuilder(uriInfo).entity(objectRestResponse).build();
	}
	
	@GET
	@Path("/{uuid}")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "根据id获取对象资源", response = Echo.class)
	@ApiResponses(value = {
	        @ApiResponse(code = 200, message = "Echo resource found"),
	    @ApiResponse(code = 404, message = "Echo resource not found")
	})
	public Response getEchoByUUID(@ApiParam(value = "Echo's uuid", required = true) @PathParam("uuid") String uuid) {
	    Echo echo = echoBiz.findByUUID(uuid);
	    if (echo == null) {
	        return Response.status(Response.Status.NOT_FOUND).entity(uuid).build();
	    }
	
	    return Response.ok(echo).build();
	}
	
}

